package com.example.demo.algorithm.test;


import com.example.demo.algorithm.linear.Stack;

/**
 * @Author: xiatian
 * @CreateTime: 2021/9/18 4:13 下午
 * @Description:
 */
public class StackMatch {
    public static void main(String[] args) {
        String a = "(北京)(上海)深圳)";
        Boolean result = isMatch(a);
        System.out.println("匹配的结果是：" + result);
    }

    private static boolean isMatch(String str) {
        Stack<String> s = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            String c = String.valueOf(str.charAt(i));
            if (c.equals("(")) {
                s.push(c);
            } else if (c.equals(")")) {
                String t = s.pop();
                if (t == null) {
                    return false;
                }
            }

        }
        if (s.isEmpty()) {
            return true;
        }

        return false;
    }
}
